package com.gamecauca.common;




import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.util.Log;

/**
 * Custom Alert Dialog. Include dialog button OK, dialog button OK and CANCEL
 * 
 * @author phuongTNM
 * @version 1.0
 */

public class CustomAlertDialog {
	public AlertDialog alertDialog;
	// private AlertDialog.Builder dialogBuilder;

	private int icon;
	private String title;
	private String message;
	private String buttonOk;
	private String buttonCancel;

	private boolean isClickOK;
	private Context context;

	// Show Log Here
	private ShowLog showLog;

	public CustomAlertDialog(Context context) {
		// TODO Auto-generated constructor stub
		// dialogBuilder = new AlertDialog.Builder(context);
		this.context = context;

//		showLog = new ShowLog("DUOC - CustomAlertDialog");

		// Set default dialog info
		Resources res = context.getResources();
		this.title = "";
		this.buttonOk = "";//res.getString(R.string.alert_dialog_button_ok);
		this.buttonCancel = "";//res.getString(R.string.alert_dialog_button_cancel);
	}

	public CustomAlertDialog(Context context, int errorCode) {

		this.context = context;
		// Set default dialog info
		Resources res = context.getResources();
		this.title = "Title";//res.getString(R.string.alert_dialog_title_error);
		this.buttonOk = "";//res.getString(R.string.alert_dialog_button_ok);
		// Client Protocol Exception
//		if (errorCode == 1) {
//			this.message = res.getString(R.string.msg_protocol_exception);
//		}
//		// Socket or Connection TimeOut Exception
//		else if (errorCode == 2) {
//			this.message = res.getString(R.string.msg_connection_timeout_exception);
//		}
//		// IO Exception
//		else if (errorCode == 3) {
//			this.message = res.getString(R.string.msg_io_exception);
//		}
	}

	// Set Icon
	public void setDialogIcon(int icon) {
		this.icon = icon;
	}

	// Set Title
	public void setDialogTitle(String title) {
		this.title = title;
	}

	// Set Message
	public void setDialogMessage(String message) {
		this.message = message;
	}

	// Set Button Ok
	public void setDialogButtonOk(String buttonOk) {
		this.buttonOk = buttonOk;
	}

	// Set Button Cancel
	public void setDialogButtonCancel(String buttonCancel) {
		this.buttonCancel = buttonCancel;
	}

	// Get flag is TRUE when click OK button
	public boolean isClickOK() {
		return this.isClickOK;
	}

	/**
	 * CallBack self it if do something after close alert dialog Use with
	 * setOnDismissListener()
	 * 
	 * @return this
	 */
	public AlertDialog callBack() {
		if (null != showLog) {
			showLog.showLogDebug("callBack :: function callback here");
		}
		return this.alertDialog;
	}

	/**
	 * Show dialog with parameter is false cancel button don't show, another.
	 * 
	 * @param hasCancelButton
	 *            (1. True: Dialog two button. 2. False: Dialog one button)
	 */
	public void showAlertDialog(boolean hasCancelButton) {
		// Set default is false when call this function
		isClickOK = false;
		// Create Alert Dialog
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);

		// We will setView layout change screen dialog here

		if (null != showLog) {
			showLog.showLogDebug("showAlertDialog :: call here");
		}

		// Don't show title when null
		if ((null != this.title) || (!this.title.equals(""))) {
			if (0 < this.icon) {
				// dialogBuilder.setIcon(this.icon);
			}
			dialogBuilder.setTitle(this.title);
		}

		dialogBuilder.setMessage(this.message);
		if (hasCancelButton) {
			dialogBuilder.setCancelable(false);
			dialogBuilder.setPositiveButton(this.buttonOk,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							// Set flag is true
							isClickOK = true;
							
					        	Log.i("DuocHV - Dialog", String.valueOf(isClickOK()));
					        
							if (null != showLog) {
								showLog.showLogDebug("showAlertDialog OK :: flag isClickOK is "	+ isClickOK());
							}
							alertDialog.cancel();
						}
					});

			dialogBuilder.setNegativeButton(this.buttonCancel,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							if (null != showLog) {
								showLog.showLogDebug("showAlertDialog CANCEL :: flag isClickOK is "	+ isClickOK());
							}
							alertDialog.cancel();
						}
					});
		} else {

			dialogBuilder.setPositiveButton(this.buttonOk,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							// Set flag is true
							isClickOK = true;
//							if (null != showLog) {
//								showLog.showLogDebug("showAlertDialog Only OK button :: flag isClickOK is "	+ isClickOK());
//							}
							alertDialog.cancel();
						}
					});
		}
		alertDialog = dialogBuilder.create();

		// Set object is null after using
		//dialogBuilder = null;

		alertDialog.show();
	}

}